package pe.com.cineplanet.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;

import pe.com.cineplanet.bean.cineplanet.Usuario;
import pe.com.cineplanet.dao.dao.UsuarioDAO;
import pe.com.cineplanet.form.UsuarioForm;

public class SeguridadController extends MultiActionController {
	
	private UsuarioForm form;
	private UsuarioDAO usuarioDAO;
	private Class<?> commandClass;  
	
	public SeguridadController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("view");
        setMethodNameResolver(resolver);
    }
	
	public UsuarioDAO getUsuarioDAO() {
		return usuarioDAO;
	}

	public void setUsuarioDAO(UsuarioDAO usuarioDAO) {
		this.usuarioDAO = usuarioDAO;
	}
	
	public ModelAndView usuarios(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/usuarios");
    } 
	
	public ModelAndView perfiles(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/perfiles");
    } 
	
	public ModelAndView permisos(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/permisos");
    } 
	
	
	public ModelAndView usuario(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/usuario");
    } 
	
	public ModelAndView perfil(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/perfil");
    } 
	
	public ModelAndView permiso(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/permiso");
    } 
	
	
	public ModelAndView nuevousuario(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/usuarionuevo");
    } 
	
	public ModelAndView nuevoperfil(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/perfilnuevo");
    } 
	
	public ModelAndView nuevopermiso(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		return new ModelAndView("seguridad/permisonuevo");
    } 
	
	public ModelAndView buscar(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
		ModelAndView view = new ModelAndView("seguridad/usuarios");

		getForm(request);
		
		String searchValue = form.getSearchValue();
		List<Usuario> productoList = usuarioDAO.findByNombre(searchValue);
	
		view.addObject("usuarioList", productoList);
		
		return view;
		
    }
	
/** Repetir si es que utiliza FORM en otros controllers ***/
	
	public Object getCommandObject(HttpServletRequest request)
	throws Exception {
		Object command = formBackingObject(request);
		bind (request, command);
		return command;
	}
	
	protected Object formBackingObject(HttpServletRequest request)
	throws Exception {
		return commandClass.newInstance();
	}

	public Class<?> getCommandClass() {
		return commandClass;
	}

	public void setCommandClass(Class<?> commandClass) {
		this.commandClass = commandClass;
	}
	
	public UsuarioForm getForm(HttpServletRequest request) throws Exception {
		Object command = getCommandObject(request);
    	form = (UsuarioForm)command;
		return form;
	}
	
}
